define(['jquery', 'knockout', 'eventbus'], function ($, ko, eventbus) {
	return function () {
		// Flag indicating if the component is visible.
		let show = ko.observable(false);
		// The heading of the alert box.
		let alertTitle = ko.observable("");
		// The body of the alert box.
        let alertText = ko.observable("");
        // Flag preventing closing of the alert  
        let preventClosingAlert = ko.observable(false);

		// Listen to the event triggered when error modals need to be displayed.
		eventbus.subscribe("showAlertEvent", function (eventData) {
			// Update the title, text with received data and display the modal.
			alertTitle(eventData.title)
			alertText(eventData.text);
			show(true);
		});

		// Callback for the modal's close button.
		let closeAlert = function () {
            if (!preventClosingAlert()) {
                show(false);
            }
            preventClosingAlert(false);
        }
        
        // On click within an alert, prevent the closing of this alert
        let onAlertClick =  function() {
            preventClosingAlert(true);
        }

		return {
			show,
			alertTitle,
			alertText,
            closeAlert,
            onAlertClick
		}
	}
});